<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="工单编码" prop=" workorderCode">
        <el-input
          v-model="queryParams.workorderCode"
          placeholder="请输入工单编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料编码" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入物料编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="specification">
        <el-input
          v-model="queryParams.specification"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数量" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="sn编码" prop="snCode">
        <el-input
          v-model="queryParams.snCode"
          placeholder="请输入sn编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- (4种情况：0，缺物料、1，无法修复、2，维修时间过长、3，报废) -->
      <el-form-item label="状态" prop="reason">
        <el-select v-model="queryParams.status" placeholder="请选择状态">
          <el-option
            v-for="item in statusList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="原因" prop="reason">
        <el-select v-model="queryParams.reason" placeholder="请选择原因">
          <el-option
            v-for="item in reasonList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="维修日期">
        <el-date-picker
          v-model="queryParams.time"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['produce:repairOrder:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['produce:repairOrder:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleDetermin"
          v-hasPermi="['produce:repairOrder:decide']"
          >判定</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="orderList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="工单编码" align="center" prop="workorderCode" />
      <el-table-column label="物料编码" align="center" prop="itemCode" />
      <el-table-column label="物料名称" align="center" prop="itemName" />
      <el-table-column label="规格型号" align="center" prop="specification" />
      <el-table-column label="数量" align="center" prop="number" />
      <el-table-column label="sn编码" align="center" prop="snCode" />
      <el-table-column label="维修人" align="center" prop="createBy" />
      <el-table-column label="维修日期" align="center" prop="createTime" />
      <el-table-column label="维修工时" align="center" prop="repairHour" />
      <el-table-column label="入库类型" align="center" prop="intypeName" />
      <el-table-column label="状态" align="center" prop="statusName" />
      <el-table-column label="原因" align="center" prop="reasonName">
      </el-table-column>
      <el-table-column label="维修类型" align="center" prop="repairTypeName" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['produce:repairOrder:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDetermin(scope.row)"
            v-hasPermi="['produce:repairOrder:decide']"
            >判定</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改维修工单 对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="sn编码" prop="snCode">
          <el-input
            v-model="form.snCode"
            placeholder="请输入sn编码"
            @keyup.enter.native="handleGetworkorderSn"
            :disabled="!!form.id"
          />
        </el-form-item>
        <el-form-item label="工单编码" prop="workorderCode">
          <el-input
            v-model="form.workorderCode"
            placeholder="请输入工单编码"
            disabled
          />
        </el-form-item>
        <el-form-item label="物料编码" prop="itemCode">
          <el-input
            v-model="form.itemCode"
            placeholder="请输入物料编码"
            disabled
          />
        </el-form-item>
        <el-form-item label="物料名称" prop="itemName">
          <el-input
            v-model="form.itemName"
            placeholder="请输入物料名称"
            disabled
          />
        </el-form-item>
        <el-form-item label="规格型号" prop="specification">
          <el-input
            v-model="form.specification"
            placeholder="请输入规格型号"
            disabled
          />
        </el-form-item>
        <el-form-item label="数量" prop="number">
          <el-input
            v-model="form.number"
            placeholder="请输入数量"
            :disabled="!!form.id"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="form.status"
            placeholder="请选择状态"
            style="width: 100%"
          >
            <el-option
              v-for="item in statusList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="原因" prop="reason">
          <el-select
            v-model="form.reason"
            clearable
            placeholder="请选择原因"
            style="width: 100%"
          >
            <el-option
              v-for="item in reasonList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维修类型" prop="repairType">
          <el-select
            v-model="form.repairType"
            placeholder="请选择维修类型"
            style="width: 100%"
          >
            <el-option
              v-for="item in repairTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item
          label="维修工时"
          prop="repairHour"
          v-if="title === title2"
        >
          <el-input v-model="form.repairHour" placeholder="请输入维修工时" />
        </el-form-item>
        <el-form-item label="入库类型" prop="inType" v-if="title === title2">
          <el-select
            v-model="form.inType"
            clearable
            placeholder="请选择入库类型"
            style="width: 100%"
          >
            <el-option
              v-for="item in inTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-show="title === title1"
          >确 定</el-button
        >
        <el-button
          type="primary"
          @click="determineForm"
          v-show="title === title2"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOrder,
  getOrder,
  addOrder,
  updateOrder,
  getworkorderSn,
} from "@/api/system/order";
import { dataTool } from "echarts";

export default {
  name: "Order",
  dicts: ["sys_yes_no"],
  data() {
    let validateReason = (rule, value, callback) => {
      setTimeout(() => {
        if (this.form.status === 1) {
          if (value === 0 || value === 1 || value === 2 || value === 3) {
            callback();
          } else {
            callback(new Error("状态是不可维修时，原因必填！"));
          }
        } else {
          callback();
        }
      }, 100);
    };
    let validateStatus = (rule, value, callback) => {
      setTimeout(() => {
        if (value === 1) {
          this.$refs.form.validateField("reason");
          callback();
        } else if (value === 0) {
          callback();
        } else {
          callback(new Error("状态必填！"));
        }
      }, 100);
    };
    return {
      selection: [],
      repairTypeList: [
        {
          value: 0,
          label: "售后维修",
        },
        {
          value: 1,
          label: "制程维修",
        },
      ],
      inTypeList: [
        {
          value: 0,
          label: "售后",
        },
        {
          value: 1,
          label: "产线",
        },
        {
          value: 2,
          label: "不良品仓",
        },
      ],
      statusList: [
        //'可维修','不可维修'
        {
          value: 0,
          label: "可维修",
        },
        {
          value: 1,
          label: "不可维修",
        },
      ],
      reasonList: [
        // reasonList:['缺物料','无法修复','维修时间过长','报废']
        {
          value: 0,
          label: "缺物料",
        },
        {
          value: 1,
          label: "无法修复",
        },
        {
          value: 2,
          label: "维修时间过长",
        },
        {
          value: 3,
          label: "报废",
        },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维修工单 表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      title1: "修改维修工单",
      title2: "判定维修工单",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workorderCode: null,
        itemCode: null,
        itemName: null,
        specification: null,
        number: null,
        snCode: null,
        status: null,
        reason: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        snCode: [
          { required: true, message: "sn编码不能为空", trigger: "blur" },
        ],
        number: [{ required: true, message: "数量不能为空", trigger: "blur" }],
        number: [{ required: true, message: "数量不能为空", trigger: "blur" }],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" },
          { validator: validateStatus, trigger: "change" },
        ],
        reason: [{ validator: validateReason, trigger: "change" }],
        repairType: [
          { required: true, message: "维修类型不能为空", trigger: "change" },
        ],
        repairHour: [
          { required: true, message: "维修工时  不能为空", trigger: "blur" },
        ],
        inType: [
          { required: true, message: "入库类型不能为空", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  mounted() {},
  methods: {
    handleGetworkorderSn() {
      console.log(this.form.snCode);
      getworkorderSn(this.form.snCode).then((res) => {
        const data = res.data ? res.data : {};
        this.form.workorderCode = data.workorderCode;
        this.form.itemCode = data.productCode;
        this.form.itemName = data.productName;
        this.form.specification = data.productSpec;
      });
    },
    /** 查询维修工单 列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then((response) => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        workorderCode: null,
        itemCode: null,
        itemName: null,
        specification: null,
        number: null,
        snCode: null,
        createBy: null,
        createTime: null,
        status: null,
        reason: null,
        updateBy: null,
        updateTime: null,
        repairType: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log(this.queryParams, "=====queryParams===");
      if (this.queryParams.time) {
        this.queryParams["createTimeBegin"] = this.queryParams.time[0];
        this.queryParams["createTimeEnd"] = this.queryParams.time[1];
      } else {
        this.queryParams["createTimeBegin"] = null;
        this.queryParams["createTimeEnd"] = null;
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.time = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.selection = selection;
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加维修工单 ";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.form = row.id
        ? this.deepClone(row)
        : this.deepClone(this.selection[0]);
      this.open = true;
      this.title = this.title1;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 判定按钮操作 */
    handleDetermin(row) {
      this.reset();
      const id = row.id || this.ids;
      this.form = row.id ? row : this.selection[0];
      this.open = true;
      this.title = this.title2;
    },
    determineForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          updateOrder(this.form).then((response) => {
            this.$modal.msgSuccess("判定成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
  },
};
</script>
